<template>
  <view class="container">
    <!-- 顶部导航栏 -->
    <view class="nav-bar">
      <img src="@/static/返回箭头.png" alt="#" @tap="goBack">
      <text class="nav-title">词书管理</text>
    </view>

    <!-- 已选词书 -->
    <view class="book-section">
      <text class="section-title">已选词书</text>
      <view class="book-list">
        <view class="book-item" v-for="book in selectedBooks" :key="book.id">
          <image :src="book.cover" class="book-cover"></image>
          <view class="book-info">
            <text class="book-name">{{ book.name }}</text>
            <text class="book-desc">{{ book.description }}</text>
            <view class="progress-bar" v-if="book.learnedWords > 0">
              <view class="progress" :style="{width: book.progress + '%'}"></view>
            </view>
            <text class="progress-text" v-if="book.learnedWords > 0">已学习
              {{ book.learnedWords }}/{{ book.totalWords }} 词
            </text>
          </view>
          <view class="action-btn remove" @tap.stop="removeBook(book)">
            <uni-icons type="minus" size="20" color="#ff4444"></uni-icons>
          </view>
        </view>
      </view>
    </view>

    <!-- 可选词书 -->
    <view class="book-section">
      <text class="section-title">可选词书</text>
      <view class="book-list">
        <view class="book-item" v-for="book in availableBooks" :key="book.id">
          <image :src="book.cover" class="book-cover"></image>
          <view class="book-info">
            <text class="book-name">{{ book.name }}</text>
            <text class="book-desc">{{ book.description }}</text>
          </view>
          <view class="action-btn add" @tap.stop="addBook(book)">
            <uni-icons type="plus" size="20" color="#4CAF50"></uni-icons>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import {api} from '@/utils/request.js';
import {getEnumValue} from "@/utils/bookName";

export default {
  data() {
    return {
      selectedBooks: [],
      availableBooks: []
    }
  },
  onShow() {
    this.loadBooks()
    this.getAllSelectedBooks()
    this.getLearnedBooks()
  },
  methods: {
    async getAllSelectedBooks() {
      const res = await api.getAllSelectedBooks();
      if (res.code === 200) {
        this.availableBooks = []
        res.result.forEach(item => {
          const description = `收录核心词汇${item.wordTotal}`
          const name = getEnumValue(item.bookType)
          this.availableBooks.push({
            "name": name,
            "description": description,
            "id": item.id,
            "type": item.bookType,
            "learnedWords": item.learnedWord,
            "totalWords": item.wordTotal
          })
        })
      }

    },
    async getLearnedBooks() {
      const res = await api.getSelectedBooks();
      if (res.code === 200) {
        this.selectedBooks = []
        res.result.forEach(item => {
          const description = `收录核心词汇${item.wordTotal}`
          const name = getEnumValue(item.bookType)
          this.selectedBooks.push({
            "name": name,
            "description": description,
            "id": item.id,
            "type": item.bookType,
            "learnedWords": item.learnedWord,
            "totalWords": item.wordTotal
          })
        })
        this.$nextTick(() => {
          const selectedBooksTypes = []
          this.selectedBooks.forEach(item => {
            selectedBooksTypes.push(item.type)
          })
          this.availableBooks = this.availableBooks.filter(item => !selectedBooksTypes.includes(item.type));
        })
      }

    },
    goBack() {
      uni.navigateBack()
    },
    loadBooks() {
      const books = uni.getStorageSync('selectedBooks')
      if (books) {
        this.selectedBooks = JSON.parse(books)
        // 从可选词书中移除已选词书
        this.availableBooks = this.availableBooks.filter(book =>
            !this.selectedBooks.some(selected => selected.id === book.id)
        )
      }
    },
    async addBook(book) {
      const res = await api.saveSelectedBooks(book.type)
      if (res.code === 200) {
        uni.showToast({
          title: '添加成功',
          icon: 'success'
        });
        await this.getAllSelectedBooks()
        await this.getLearnedBooks()
      } else {
        uni.showToast({
          title: '添加失败',
          icon: 'error'
        });
      }
    },
    removeBook(book) {
      // 如果有学习进度，提示确认
      if (book.learnedWords > 0) {
        uni.showModal({
          title: '提示',
          content: '删除词书将清空学习进度，确定要删除吗？',
          success: (res) => {
            if (res.confirm) {
              this.doRemoveBook(book)
            }
          }
        })
      } else {
        this.doRemoveBook(book)
      }
    },
    async doRemoveBook(book) {
      const res = await api.removeLearnBook(book.type)
      if (res.code === 200) {
        uni.showToast({
          title: '移除成功',
          icon: 'success'
        });
        await this.getAllSelectedBooks()
        await this.getLearnedBooks()
      } else {
        uni.showToast({
          title: '移除失败',
          icon: 'error'
        });
      }
    }
  }
}
</script>

<style lang="scss">
.container {
  min-height: 100vh;
  background: #f5f5f5;
  padding-bottom: 40rpx;
  .nav-bar {
    img {
      height: 40rpx;
      width: 40rpx;
    }

    border-radius: 20rpx;
    background: #fff;
    height: 88rpx;
    //display: flex;
    //align-items: center;
    padding: 30rpx;
    //width: calc(100% - 30rpx);
    position: relative;

    .back-icon {
      padding: 20rpx;
      margin-left: -20rpx;
    }

    .nav-title {
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
      font-size: 34rpx;
      font-weight: 500;
      color: #333;
    }
  }
}



.book-section {
  margin: 20rpx;
  padding: 30rpx;
  background: #fff;
  border-radius: 20rpx;

  .section-title {
    font-size: 32rpx;
    font-weight: bold;
    color: #333;
    margin-bottom: 20rpx;
    display: block;
  }

  .book-list {
    display: flex;
    flex-direction: column;
    gap: 20rpx;
  }

  .book-item {
    display: flex;
    align-items: center;
    padding: 20rpx;
    background: #f8f8f8;
    border-radius: 15rpx;

    .book-cover {
      width: 100rpx;
      height: 140rpx;
      border-radius: 10rpx;
      margin-right: 20rpx;
      box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
    }

    .book-info {
      flex: 1;

      .book-name {
        font-size: 32rpx;
        font-weight: 500;
        color: #333;
        margin-bottom: 10rpx;
        display: block;
      }

      .book-desc {
        font-size: 24rpx;
        color: #666;
        margin-bottom: 15rpx;
      }

      .progress-bar {
        height: 6rpx;
        background: #eee;
        border-radius: 3rpx;
        margin-bottom: 10rpx;

        .progress {
          height: 100%;
          background: #4CAF50;
          border-radius: 3rpx;
          transition: width 0.3s;
        }
      }

      .progress-text {
        font-size: 24rpx;
        color: #999;
      }
    }

    .action-btn {
      width: 60rpx;
      height: 60rpx;
      border-radius: 30rpx;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-left: 20rpx;

      &.add {
        background: rgba(76, 175, 80, 0.1);
      }

      &.remove {
        background: rgba(255, 68, 68, 0.1);
      }
    }
  }
}
</style> 